#include "helpview.h"
#include "item/imageitem.h"
#include "item/longitem.h"
#include <QLocale>
#include "resources.h"
#include <QGraphicsSceneMouseEvent>
#include "sound/GameSounds.h"

const int INTERVAL = 2;

enum HelpBtn
{
    BACK_HB,
    NULL_HB,
}helpBtn(NULL_HB);

HelpView::HelpView(QObject *parent) :
    QGraphicsScene(parent)
{
    setSceneRect(0.0, 0.0, 640.0, 360.0);
    m_timer.setInterval(100);
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(roll()));

    m_bgItem = new ImageItem(QImage(":/image/help/bg.jpg"),
                             QRect(QPoint(0,0),QImage(":/image/help/bg.jpg").size()));
    m_bgItem->setZValue(0.0);
    addItem(m_bgItem);

    m_longItem = new LongItem();
    m_longItem->setZValue(1.0);
    m_longItem->setPos(148.0,80.0);
    addItem(m_longItem);

    m_backItem = new ImageItem(*resources::m_selBackImage,
                               QRect(QPoint(0,0),resources::m_selBackImage->size()));
    m_backItem->setZValue(1.0);
    m_backItem->setPos(482.0,283.0);
    addItem(m_backItem);
}

void HelpView::IsShowHelp(bool bShow)
{
    m_bhelp = bShow;
    m_longItem->m_bhelp = bShow;
    if(bShow)
    {
        switch(resources::m_iLanguage)
        {
        case QLocale::Chinese:
        case QLocale::HongKong:
        case QLocale::Taiwan:
        default:
        {
            m_longItem->m_image = *resources::m_helphelpImage;
            m_longItem->setPos(148.0,80.0);
        }
        break;
        }
    }
    else
    {
        switch(resources::m_iLanguage)
        {
        case QLocale::Chinese:
        case QLocale::HongKong:
        case QLocale::Taiwan:
        default:
        {
            m_longItem->m_image = *resources::m_helpaboutImage;
            m_longItem->setPos(131.0,80.0);
        }
        break;
        }
    }

    if(m_timer.isActive())
    {
        m_timer.stop();
    }

    m_longItem->m_y = 0;
    m_timer.start();
}

void HelpView::roll()
{
    if(m_bhelp)
    {
        if(m_longItem->m_y > 563)
        {
            m_longItem->m_y = 563;
            m_timer.stop();
        }
        else
        {
            m_longItem->m_y += INTERVAL;
        }
    }
    else
    {
        if(m_longItem->m_y > 563)
        {
            m_longItem->m_y = 563;
            m_timer.stop();
        }
        else
        {
            m_longItem->m_y += INTERVAL;
        }
    }
}

void HelpView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    m_timer.stop();
    if(m_backItem->isUnderMouse())
    {
        if(resources::m_bplaysound)
        {
            resources::m_sound->play(5);
        }
        m_backItem->setImage(*resources::m_selBackpImage);
        helpBtn = BACK_HB;
    }
    else
    {
        helpBtn = NULL_HB;
        m_y = event->scenePos().y();
    }
}

void HelpView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    m_longItem->m_y -= event->scenePos().y() - m_y;
    if(m_longItem->m_y < 0)
    {
       m_longItem->m_y = 0;
    }
    else if(m_longItem->m_y > 563)
    {
        m_longItem->m_y = 563;
    }
    m_y = event->scenePos().y();
}

void HelpView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    m_timer.start();
    switch(helpBtn)
    {
    case BACK_HB:
    {
        m_backItem->setImage(*resources::m_selBackImage);
        if(m_backItem->isUnderMouse())
            resources::SetMain();
    }
    break;
    default:
        break;
    }
}
